﻿using FairyGUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class PackItemPage : BasePage
{
    private GList lsItems;

    private GTextField tfItemDesc;

    private GTextField tfOrigin;

    private GButton tfSell;

    private GButton tfUse;

    public PackItemPage(GComponent vie) : base(vie)
    {
        lsItems = this.view.GetChild("n1").asList;
        tfItemDesc = this.view.GetChild("n2").asTextField;
        tfOrigin = this.view.GetChild("n3").asTextField;
        tfSell = this.view.GetChild("n4").asButton;
        tfUse = this.view.GetChild("n5").asButton;

        lsItems.onClickItem.Add(OnPackItemClick);
    }

    public override void Show()
    {
        MsgListenerMgr.AddMsgListener(MsgListenerType.PackItemChange, OnPackItemChange);
        this.Load();
    }

    private void OnPackItemChange(object[] para)
    {
    }

    public override void Close()
    {
        MsgListenerMgr.RemoveMsgListener(MsgListenerType.PackItemChange, OnPackItemChange);
    }

    private void Load()
    {
        lsItems.RemoveChildrenToPool();

        List<Item> items = GameNetwork.Player.PackItemMgr.GetAll();
        items.Sort(ItemComparison);
        for (int i = 0; i < items.Count; i++)
        {
            if (items[i].TryGetStaticInfo(out InitItem initItem))
            {
                GButton button = lsItems.GetFromPool("ui://f2iijc3nochobf").asButton;
                button.data = initItem.id;

                GTextField title = button.GetChild("n2").asTextField;
                title.text = initItem.name;
                title.color = CommonUtil.GetItemColor(initItem.quality);

                GTextField tfCount = button.GetChild("n3").asTextField;
                tfCount.text = items[i].Count.ToString();
                lsItems.AddChild(button);
            }
        }
        SetInfo(null);
    }

    private int ItemComparison(Item a, Item b)
    {
        int pa = 0;
        if (a.TryGetStaticInfo(out InitItem initItemA))
        {
            pa = initItemA.quality;
        }
        int pb = 0;
        if (b.TryGetStaticInfo(out InitItem initItemB))
        {
            pb = initItemB.quality;
        }
        if (pa > pb)
        {
            return -1;
        }
        else if (pa < pb)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

    private void SetInfo(InitItem initItem)
    {
        if (initItem == null)
        {
            tfItemDesc.visible = false;
            tfOrigin.visible = false;
            tfSell.visible = false;
            tfUse.visible = false;
            return;
        }
        else
        {
            tfItemDesc.visible = true;
            tfOrigin.visible = true;
            
            tfSell.visible = initItem.is_sale;
            tfUse.visible = initItem.is_use;
        }

        tfItemDesc.text = initItem.info;

        if (string.IsNullOrEmpty(initItem.source))
        {
            tfOrigin.visible = false;
        }
        else
        {
            tfOrigin.visible = true;
            tfOrigin.SetVar("text", initItem.source).FlushVars();
        }
    }

    private void OnPackItemClick(EventContext context)
    {
        GButton button = lsItems.GetChildAt(lsItems.selectedIndex).asButton;
        if (button == null)
        {
            return;
        }

        long id = (long)button.data;
        if (InitItemMgr.Dict.TryGetValue(id, out InitItem initItem))
        {
            SetInfo(initItem);
        }
    }
}
